﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;

namespace Amazon.Route53.DataContracts
{
    [DataContract(Namespace = Constants.Namespace)]
    public class ResourceRecordSet:  IComparable<ResourceRecordSet>
    {
        /// <summary>
        /// The name you wish to perform the action on.
        /// This must be a fully-specified name, 
        /// ending with a final period as the last label indication. 
        /// If you omit the final period, Amazon Route 53 assumes the name is relative 
        /// to the root.
        /// </summary>
        [DataMember(Order=1)]
        public String Name { get; set; }

        /// <summary>
        /// The domain name type
        /// </summary>
        [DataMember(Order = 2)]
        public DomainNameType Type { get; set; }

        /// <summary>
        /// The resource record cache time to live (TTL), in seconds.
        /// </summary>
        [DataMember(Order=3)]
        public int TTL { get; set; }

        /// <summary>
        /// Information about the resource records to act upon.
        /// </summary>
        [DataMember(Order = 4)]
        public ResourceRecord[] ResourceRecords { get; set; }

        #region IComparable<ResourceRecordSet> Members

        public int CompareTo(ResourceRecordSet other)
        {
            if (this.TTL != other.TTL)
                return 1;
            //In case where Current Resource Record items are less than other resource record.
            //Matching loop will not be able to catch the extra records.
            if (this.ResourceRecords.Length != other.ResourceRecords.Length)
                return 1;

            foreach (var aResourceRecord in this.ResourceRecords)
            {
                ResourceRecord aRecord = other.ResourceRecords.FirstOrDefault(t => t.Value == aResourceRecord.Value);
                if (aRecord == null)
                    return 1;
            }

            return 0;
        }

        #endregion

        public override bool Equals(object obj)
        {
            var other = obj as ResourceRecordSet;
            if (other == null) return false;

            return this.Name.Equals(other.Name, StringComparison.OrdinalIgnoreCase) && this.Type == other.Type &&  CompareTo(other) == 0;
        }

        public override int GetHashCode()
        {
            return base.GetHashCode();
        }

    }

    [DataContract(Namespace = Constants.Namespace)]
    public class ResourceRecord
    {
        /// <summary>
        ///The current or new value, not to exceed 4,000 characters. 
        ///In the case of a DELETE action, if the current value does not match the actual value, 
        ///an error is returned.
        /// </summary>
        [DataMember]
        public String Value { get; set; }
    }   
    
}
